import * as echarts from '../../ec-canvas/echarts';

const app = getApp();
var chart;
function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    title: {
      text: '销售菜品比例',
      left: 'center',
      top: '10',
      padding: 10,

      textStyle: {
        fontSize: 18,
        fontStyle: 'normal',
        fontWeight: 'normal',
      },
    },

    color: ['#7cbf15', '#ff3111', '#ff9c00', '#00fce8', '#b412d5', '#1baa40', '#00b5a2'],
    series: [{
      center: ['50%', '60%'],
      type: 'pie',
      radius: '65%',
      data: [{
          value: 1048,
          name: '热菜'
        },
        {
          value: 735,
          name: '酒水'
        },
        {
          value: 580,
          name: '凉菜'
        },
        {
          value: 484,
          name: '甜品'
        },
        {
          value: 300,
          name: '西餐'
        },
        {
          value: 300,
          name: '中餐'
        }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }]
  };

  chart.setOption(option);
  return chart;
}


// var app = getApp();
Page({
  onShareAppMessage: function(res) {
    return {
      title: 'ECharts 可以在微信小程序中使用啦！',
      path: '/pages/index/index',
      success: function() {},
      fail: function() {}
    }
  },
  data: {
    nvabarData: {
      showCapsule: 1,
      showSelect: 0,
      title: '',
    },
    statusBarHeight: app.globalData.statusBarHeight * 2 + 20,
    ec: {
      onInit: initChart
    },
    step: 0,
    viewId: 0,
    tcbox:true,
    zz:true,
  },
  close:function(){
    this.setData({
      tcbox:true,
      zz:true,
    })
  },
  select: function (e) {
    var t = this;
    this.setData({
      viewId: e.currentTarget.dataset.index
    })
    var type = Number(e.currentTarget.dataset.index)+1;
    app.visit("account/getStorePinFenTj", {
      store_id: t.data.store_id,
      user_id:11,
      type:type
    }, "", function(c) {
      console.log(c)
      if (0 == c.code) {
        t.setData({
          yinye: c.data.yinye,
          huiyuans: c.data.huiyuans,
          kaitail: c.data.kaitail,
          renjunxf: c.data.renjunxf,
          baobend: c.data.baobend,
          step: c.data.baobend.fenshu,
          fenshu: c.data.baobend.fenshu,
          xianshi: c.data.baobend.xianshi,
          xianshiz: c.data.baobend.xianshiz,
        })
        t.fenZhi();
      } else {
        app.showToast(c.msg)
      }
    })
  },
  fenZhi: function() {
    var me = this;
    var cxt = wx.createCanvasContext('canvasCircle');
    cxt.setLineWidth(5);
    cxt.setStrokeStyle('#f6f6f6');
    cxt.setLineCap('round');
    cxt.beginPath();
    cxt.arc(55, 55, 48, 0, 2 * Math.PI, false);
    cxt.stroke();
    cxt.draw();
    
    //加载动画
    var steps = 1,
      startAngle = 1.5 * Math.PI,
      endAngle = 0,
      speed = 100,
      sec = 10;

    function drawing(s, e) {
      var context = wx.createCanvasContext('canvasRing');
      var gradient = cxt.createLinearGradient(50, 25, 25, 50);
      context.setLineWidth(10);
      context.setStrokeStyle(gradient);
      context.setLineCap('round');
      context.beginPath();
      context.arc(55, 55, 48, s, e, false);
      gradient.addColorStop("0", "#cc2221");
      gradient.addColorStop("1.0", "#a21b1b");
      context.stroke();
      context.draw();
    }

    function drawings(s, e) {
      var context = wx.createCanvasContext('canvasRing');
      var gradient = cxt.createLinearGradient(50, 25, 25, 50);
      context.setLineWidth(10);
      context.setStrokeStyle(gradient);
      context.setLineCap('round');
      context.beginPath();
      context.arc(55, 55, 48, s, e, false);
      gradient.addColorStop("0", "#ff773a");
      gradient.addColorStop("1.0", "#fa4d27");
      context.stroke();
      context.draw();
    }

    function drawingss(s, e) {
      var context = wx.createCanvasContext('canvasRing');
      var gradient = cxt.createLinearGradient(50, 25, 25, 50);
      context.setLineWidth(10);
      context.setStrokeStyle(gradient);
      context.setLineCap('round');
      context.beginPath();
      context.arc(55, 55, 48, s, e, false);
      gradient.addColorStop("0", "#f99ab0");
      gradient.addColorStop("1.0", "#e06c86");
      context.stroke();
      context.draw();
    }

    function drawingsss(s, e) {
      var context = wx.createCanvasContext('canvasRing');
      var gradient = cxt.createLinearGradient(50, 25, 25, 50);
      context.setLineWidth(10);
      context.setStrokeStyle(gradient);
      context.setLineCap('round');
      context.beginPath();
      context.arc(55, 55, 48, s, e, false);
      gradient.addColorStop("0", "#2de7bc");
      gradient.addColorStop("1.0", "#20ba96");
      context.stroke();
      context.draw();
    }

    function drawLoading() {
      if (steps < me.data.fenshu) {
        me.setData({
          step: steps + 1
        })
        endAngle = steps * 2 * Math.PI / speed + startAngle;
        if (endAngle < 8.41946) {
          drawing(startAngle, endAngle);
        } else if (endAngle < 9.676 ) {
          drawings(startAngle, endAngle);
        } else if ( endAngle < 10.9327) {
          drawingss(startAngle, endAngle);
        } else {
          drawingsss(startAngle, endAngle);
        }
        steps++;
        console.log(endAngle);
      } else {
        clearInterval(this.interval);
      }
    }
    this.interval = setInterval(drawLoading, sec);

    console.log(sec);
  },
  onLoad: function(options) {
    var qubie = options.qubie;
    if(qubie == 1){
      this.getStoreInfosern();
    }
  },

  onShow: function() {
    var store_id = wx.getStorageSync("store_id");
    console.log(store_id)
    if (!store_id) {
      this.setData({
        store_id: 0
      })
      app.showToast("请登录小程序")

    } else {
      this.setData({
        store_id: wx.getStorageSync("store_id")
      })
      this.getProfitSituation();
      this.getFoodPrint();
      this.getStorePinFenTj();
    }
  },

  getStorePinFenTj: function() {
    var t = this;
    app.visit("account/getStorePinFenTj", {
      store_id: t.data.store_id,
      user_id:11,
      type:1
    }, "", function(c) {
      console.log(c)
      if (0 == c.code) {
        t.setData({
          yinye: c.data.yinye,
          huiyuans: c.data.huiyuans,
          kaitail: c.data.kaitail,
          renjunxf: c.data.renjunxf,
          baobend: c.data.baobend,
          step: c.data.baobend.fenshu,
          fenshu: c.data.baobend.fenshu,
          xianshi: c.data.baobend.xianshi,
          xianshiz: c.data.baobend.xianshiz,
        })
        t.fenZhi();
      } else {
        app.showToast(c.msg)
      }
    })
  },

  getStoreInfosern:function(){
    var t = this;
    app.visit("account/getUserStore", {
      store_id: wx.getStorageSync("store_id")
    }, "", function(c) {
      console.log(c)
      if (0 == c.code) {
        t.setData({
          contentsd:c.data.dltishiyu,
          tcbox:false,
          zz:false,
        })
      }
    })
  },

  getFoodPrint: function() {
    var t = this;
    app.visit("account/getFoodPrint", {
      store_id: t.data.store_id
    }, "", function(c) {
      console.log(c)
      if (0 == c.code) {
        var option = {
          title: {
            text: '销售菜品比例',
            left: 'center',
            top: '10',
            padding: 10,

            textStyle: {
              fontSize: 18,
              fontStyle: 'normal',
              fontWeight: 'normal',
            },
          },

          color: ['#7cbf15', '#ff3111', '#ff9c00', '#00fce8', '#b412d5', '#1baa40', '#00b5a2'],
          series: [{
            center: ['50%', '60%'],
            type: 'pie',
            radius: '65%',
            data: c.data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }]
        };
        setTimeout(()=>{
          chart.clear()
          chart.setOption(option);
        },100)
      } else {
        app.showToast(c.msg)
      }
    })
  },

  getProfitSituation: function() {
    var t = this;
    app.visit("account/getUserStore", {
      store_id: t.data.store_id
    }, "", function(c) {
      console.log(c)
      if (0 == c.code) {
        t.setData({
          info: c.data
        })
      } else {
        app.showToast(c.msg)
      }
    })
  },

  zongsy: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: 'zongsy'
      });
    }
  },
  xianshangsy: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: 'xianshangsy'
      });
    }
  },

  fanpiansy: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: 'fanpiansy'
      });
    }
  },

  xianxiasy: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: 'xianxiasy'
      });
    }
  },
  xianxiayue: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: 'xianxiayue'
      });
    }
  },

  jinjian: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: 'Merchant_info'
      });
    }
  },

  bataisy: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: 'bataisy'
      });
    }
  },
  hongbao: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: 'hongbao'
      });
    }
  },
  login: function(e) {
    wx.navigateTo({
      url: 'login'
    });
  },

  order: function(e) {
    var store_id = wx.getStorageSync("store_id");
    if (!store_id) {
      app.showToast("请登录")
    } else {
      wx.navigateTo({
        url: '/pages/new/order'
      });
    }
  },
  tuichu: function(e) {
    // app.visit("account/endLogin", {
    //   account_id:wx.getStorageSync("account_id")
    // }, "", function(c) {
    //   console.log(c)
    //   if(0 == c.code){
    //     wx.setStorageSync("store_id","");
    //     wx.setStorageSync("account_id","");
    //     wx.navigateTo({
    //       url: 'index'
    //     });
    //   }else{
    //     app.showToast(c.msg)

    //   }
    // })
    wx.navigateTo({
      url: 'jiaojieb'
    });
  },

  zhuotaigl: function(e) {
    wx.navigateTo({
      url: 'zhuotaigl'
    });
  },
  tixiangl: function(e) {
    wx.navigateTo({
      url: 'tixiangl'
    });
  },
  huiyuan: function(e) {
    wx.navigateTo({
      url: 'huiyuan'
    });
  },
  huodgl: function(e) {
    wx.navigateTo({
      url: 'huodgl'
    });
  },
  shouyiqk: function(e) {
    wx.navigateTo({
      url: 'shouyiqk'
    });
  },
  zhenduanbg: function (e) {
    wx.navigateTo({
      url: 'zhenduanbg'
    });
  },
  getScancode: function () {
    var t = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        wx.showModal({
          title: '提示',
          content: '确定核销吗',
          success (res2) {
            if (res2.confirm) {
              var result = res.result;
              app.visit("account/hxOrder", {
                store_id: t.data.store_id,
                result:result
              }, "", function(c) {
                console.log(c)
                app.showToast(c.msg,'none',3000)
              })
            } else if (res2.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      },
      fail:(res)=>{
        app.showToast("调用核销扫码失败",'none',3000)
      }
    })
  },
  gmbanben: function (e) {
    wx.navigateTo({
      url: 'gmbanben'
    });
  },
  zhuche:function (e) {
    wx.navigateTo({
      url: 'register'
    });
  },
  wanshan:function (e) {
    wx.navigateTo({
      url: 'perfect_info?store_id='+this.data.store_id
    });
  }
})